<html>
<head>
<title>Martyn's Slider Plaything</title>
<script type="text/javascript">
function adjust(f, ip)
{
	var sum = 0;	// sum of the bits that need rescaling
	var numSliders = f.length;	// number of input sliders (may need  some jiggering if we add other controls)
	for(var i=0; i<numSliders; i++ )
	{
		if( f.elements[i] != ip )	// the non- selected slider controls
			sum += parseFloat(votes[i]);
		else
			votes[i] = ip.value;	// set the vote in the slider that was 
	}
	if(sum != 0.0)
		scale = (100.0-ip.value)/sum;	// scale non-touched ones by this amount
	else
		scale = (100-ip.value)/numSliders	// no other non-zero sliders so spread out extra % evenly
	for(var i=0; i<numSliders; i++ )	// now assign the percentages to the non-moved sliders (sliders may not add up to 100, votes (very nearly) will
	{
		if(f.elements[i] != ip)
		{
			if(sum != 0.0)
				votes[i] *= scale;	// only the non-zero ones
			else
				votes[i] = scale;	// all the rest at zero so equally
			f.elements[i].value = votes[i];	// set the sliders
		}
	}
}
function loadXMLDoc()	// see http://www.w3schools.com/Ajax/ajax_xmlhttprequest_response.asp
{
	var str="";
	for(i=0; i<votes.length; i++)
	{
		num = parseInt(votes[i]*10)/10;	// reduce to 1dp
		str += Things[i]+"="+num+"&";
	}
	str = str.substr(0, str.length-1);

	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
			document.getElementById("myDiv").innerHTML += xmlhttp.responseText + "<br />";
/*		if (xmlhttp.readyState==1)
			document.getElementById("myDiv").innerHTML += "1: server connection established" + "<br />";
		if (xmlhttp.readyState==2)
			document.getElementById("myDiv").innerHTML += "2: request received" + xmlhttp.getResponseHeader("Content-Type") + "<br />";
		if (xmlhttp.readyState==3)
			document.getElementById("myDiv").innerHTML += "3: processing request" + "<br />";
		if (xmlhttp.readyState==4)
			document.getElementById("myDiv").innerHTML += "4: request finished and response is ready " + xmlhttp.status + xmlhttp.statusText + "<br />";*/
	}
	xmlhttp.open("POST","http://penguin.uclan.ac.uk/~mjshaw/vote/vote.php",true);	//,"access","access");
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(str);
}

</script>
</head>
<body>
<h1>Rate these Course titles</h1>
<form>
<script type="text/javascript">
	var Things = ["BSc(Hons) Computer Graphics for Film and TV", "BSc(Hons) Graphics for Film and TV", "BSc(Hons) News Graphics", "BSc(Hons) Infographics"];
	var votes = new Array();	// keep this as an array of floats
	var init = 100.0/Things.length
	for(i = 0; i < Things.length; i++)
	{
		document.write('<input type="range" min="0" max="100" value="'+init+'" step="1" onChange="adjust(this.form, this)" /> ');
		document.write(Things[i]+'<br />');
		votes.push(init);
	}
</script>
</form>
<button type="button" onclick="loadXMLDoc()">Submit</button>
<div id="myDiv">Submissions:<br /></div>
</body>
</html>